﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using eStock.hControl;
using HCommon;
namespace eStock
{
    public partial class frmUser_Add : hDialog
    {
        public frmUser_Add()
        {
            InitializeComponent();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (btnSave.Error == true) { btnSave.Error = false; return; }
            SaveMaster();
        }

        private void SaveMaster()
        {
            try
            {
                if (txtUserCode.Visible == true && txtUserCode.Text.Trim().Length > 0 && Dao.Exist("Select Top 1 1 From tblUser with(nolock) where user_code = '" + Common.xoaNhay(txtUserCode.Text) + "' and user_name <> '" + Common.xoaNhay(txtUser.Text) + "'"))
                {
                    Dialog.ShowMess(Dialog.mType.warn, "Đã có nhân viên kinh doanh khác sử dụng mã code " + txtUserCode.Text + ". Vui lòng chọn một mã code khác.");
                    return;
                }
                int rs = 0;
                if (this.FormMode == Mode.Insert)
                {
                    rs = tblUSER.Insert(txtUserFullName.Text, txtUser.Text, txtUserPwd.Text, cboGroup.SelectedValue.ToString(), cboDEP.SelectedValue.ToString(), txtUserCode.Text);
                }
                else
                {
                    rs = tblUSER.Update(txtUserFullName.Text, txtUser.Text, txtUserPwd.Text, cboGroup.SelectedValue.ToString(), cboDEP.SelectedValue.ToString(), txtUserCode.Text);
                }

                Dialog.ShowMess(rs);

                this.UpdateKey = txtUser.Text;
                this.FormMode = Mode.Update;
                LoadMaster();
            }
            catch (Exception ex)
            {
                Common.GhiLog("SaveMaster", ex);
                Dialog.ShowMess(Dialog.mType.err, ex.Message);
            }
        }

        private void LoadMaster()
        {
            try
            {
                if (this.FormMode == Mode.Insert)
                {
                    txtUser.ReadOnly = false;
                    Common.ClearControl(grdUser);
                    return;
                }

                txtUser.ReadOnly = true;
                DataTable dt = new DataTable();
                dt = Dao.Get_Table("SELECT * FROM tblUser with(nolock) where User_Name = N'" + this.UpdateKey + "'");
                if (dt.Rows.Count == 0)
                {
                    Dialog.ShowMess(Dialog.mType.err, "Tài khoản này đã bị xóa!");
                    this.Close();
                    return;

                }
                txtUserFullName.Text = dt.Rows[0]["user_fullname"].ToString();
                txtUser.Text = dt.Rows[0]["user_name"].ToString();
                txtUserPwd.Text = dt.Rows[0]["user_pwd"].ToString();
                cboGroup.SelectedValue = dt.Rows[0]["group_cd"].ToString();
                cboDEP.SelectedValue = dt.Rows[0]["dept_cd"].ToString();
                txtUserCode.Text = dt.Rows[0]["user_code"].ToString();

            }
            catch (Exception ex)
            {
                Common.GhiLog("LoadMaster", ex);
                Dialog.ShowMess(Dialog.mType.err, ex.Message);
            }
        }

        private void frmUser_Add_Load(object sender, EventArgs e)
        {
            LoadMaster();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cboDEP_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (cboDEP.SelectedValue.ToString().Equals("DEP00002"))
                {
                    txtUserCode.Visible = true;
                    txtUserCode.NotEmpty = true;
                }
                else
                {
                    txtUserCode.Visible = false;
                    txtUserCode.NotEmpty = false;
                    txtUserCode.Text = string.Empty;
                }
            }
            catch { }
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            this.FormMode = Mode.Insert;
            LoadMaster();
        }
    }
}
